angular.module('mamApp').controller('anchorTagManageController', ['$scope', 'TagsManager', '$state', '$uibModal', 'ImsDialog','BaseValidations',
    function ($scope, TagsManager, $state, $uibModal, ImsDialog,BaseValidations) {
        $scope.search = {
            dimensionId:$state.params.dimensionId,
            anchortagName:$state.params.anchortagName,
            pageNo: $state.params.pageNo || 1,
            pageSize: $state.params.pageSize || 10
        }
        $scope.BaseValidations=BaseValidations;

        TagsManager.anchorDimensionsDetail($scope.search.dimensionId).then(function (result) {
            if(result.data.c=="s"){
                $scope.dimensionDetail=result.data.anchordimensionVO;
            }
        })

        $scope.keyword=$state.params.anchortagName;

        $scope.searchClick=function (keyword) {
            $scope.search.anchortagName=keyword;
            $state.go($state.current.name,$scope.search)
        }

        $scope.paginationConf = {
            currentPage: $state.params.pageNo || 1,
            itemsPerPage: $state.params.pageSize || 10,
            pagesLength: 9,
            search: 0,
            onChange: function () {
                $scope.search.pageNo = this.currentPage;
                $scope.search.pageSize = this.itemsPerPage;
                $state.go($state.current.name, $scope.search)
            }
        }

        TagsManager.cdanchorTags($scope.search.dimensionId,[$scope.search.pageNo,$scope.search.pageSize],$scope.search).then(function (result) {
            $scope.tags=result.data.list;
            $scope.paginationConf.totalItems=result.data.total;

        })
        $scope.allCheckChange = function (allCheck) {
            $scope.tags.forEach(function (item) {
                item.checked = allCheck;
            })
        }

        $scope.deleteTag= function (id) {
            ImsDialog.open({title: "标签删除", message: "确定删除该标签？"}).then(function (a) {
                if (a == "ok") {
                    TagsManager.anchorTagsDelete([id]).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })
        }

        var selectIds = function () {
            var ids = [];
            $scope.tags.forEach(function (item) {
                if (item.checked) {
                    ids.push(item.anchorId);
                }
            })
            return ids;
        }


        $scope.selectChangeStatus = function (status) {

            if (!selectIds().length) {
                return false;
            }
            ImsDialog.open({
                title: "通用标签" + (status == 2 ? '禁用' : '启用'),
                message: "确定" + (status == 2 ? '禁用' : '启用') + "选中的标签？"
            }).then(function (a) {
                if (a == "ok") {
                    TagsManager.anchorTagStatusChange(selectIds(), {status: status}).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })
        }

        $scope.selectDelete =function () {
            if (!selectIds().length) {
                return false;
            }
            ImsDialog.open({title: "纬度删除", message: "确定删除选中的标签纬度？"}).then(function (a) {
                if (a == "ok") {
                    TagsManager.anchorTagsDelete(selectIds()).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })
        }


        $scope.itemCheckedChange = function () {

            if (selectIds().length == $scope.tags.length) {
                $scope.allChecked=true;
            }else{
                $scope.allChecked=false;
            }

        }
        $scope.addCommonTag=function (tag) {
            var modal = $uibModal.open({
                templateUrl: 'anchorTagModal.html',
                controller: 'anchorTagsModalController',
                size: 'normal',
                openedClass:"modal-open-normal",
                resolve: {
                    param:function () {
                        return {dimensionId:$scope.search.dimensionId,dimensionsName:$scope.dimensionDetail.dimensionsName,tag:tag};
                    }
                }
            });
            modal.result.then(function (result) {
                if(result=="ok"){
                    setTimeout(function () {
                        $state.reload();
                    },200)
                }
            });
        }


        $scope.changeStatus = function (item) {
            var status = 2;
            if (item.status == 2) {
                status = 4;
            } else {
                status = 2;
            }
            ImsDialog.open({
                title: "标签" + (status == 2 ? '禁用' : '启用'),
                message: "确定" + (status == 2 ? '禁用' : '启用') + "该标签？"
            }).then(function (a) {
                if (a == "ok") {
                    TagsManager.anchorTagStatusChange([item.anchorId], {status: status}).then(function (result) {
                        if (result.data.c == "s") {
                            $state.reload();
                        }
                    })
                }
            })

        }

        $scope.keyup=function (e,keyword) {
            if(e.key=="Enter"){
                $scope.search.pageNo=1;
                $scope.search.anchortagName=keyword;
                $state.go($state.current.name,$scope.search)
            }
        }



    }])

